package domain;

import java.util.Calendar;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

import domain.Confirmation.Calification;
import domain.exceptions.RequiredFieldsException;

@Entity
@Table(name = "usuario")
public class User extends PersistentEntity{
	private String userName;
	private String password;
	private String name;
	private String lastName;
	private boolean isBlock;
	
	@Column(nullable = false)
	private Date lastAccess;
	
	@Column(nullable = false)
	private String question;
	
	@Column(nullable = false)
	private String answer;
	
	@Column(nullable = false)
	private String address;
	
	@Column(nullable = false)
	private String telephone;
	
	@Column(nullable = false)
	private String celular;
	
	@Column(nullable = false)
	private Date birthday;
	
	@Column(nullable = false)
	private boolean isMale;
	
	@Column(nullable = false)
	private boolean admin;
	
	@Column(nullable = false)
	private String email;
	
	private int positive;
	private int negative;
	private int neutral;
	
	//hibernate
	public User(){
	}
	
	public User(String userName, String password, String name, String lastName, String email, 
			String question, String answer, String address, String telephone, String celular, 
			Date birthday, boolean isMale, boolean admin){
		this(userName,password,name,lastName,email,null,false,question,answer,address,telephone,celular,birthday,isMale, admin);
	}

	public User(String userName, String password, String name, String lastName, String email, 
			Date lastAccess, boolean isBlock, String question, String answer, String address,
			String telephone, String celular, Date birthday, boolean isMale, boolean admin){
		Date currDate = new Date(Calendar.getInstance().getTime().getTime());
		
		if(userName==null || password==null || name==null || lastName==null || email==null ||
				question==null || answer==null || telephone==null || celular==null || birthday==null)
			throw new RequiredFieldsException();
		
		if(userName.isEmpty() || password.isEmpty() || name.isEmpty() || lastName.isEmpty() ||
				email.isEmpty() ||  question.isEmpty() || answer.isEmpty()
				|| telephone.isEmpty() || celular.isEmpty() )
			throw new IllegalArgumentException();
		if(lastAccess==null){
			this.lastAccess=currDate;
		}else{
			this.lastAccess=lastAccess;
		}
		this.userName = userName;
		this.password = password;
		this.name = name;
		this.lastName = lastName;
		this.email = email;
		this.isBlock=isBlock;
		this.question = question;
		this.answer = answer;
		this.address = address;
		this.telephone = telephone;
		this.celular = celular; 
		this.birthday = birthday;
		this.isMale = isMale;
		this.admin = admin;
	}	
	
//	public void update(String password, String address, String answer, Date birthday, String celular, 
//			String email, String lastName, boolean isMale, String name, String question, String telephone){
//		
//		if(userName==null || password==null || name==null || lastName==null || email==null ||
//				question==null || answer==null || telephone==null || celular==null || birthday==null)
//			throw new RequiredFieldsException();
//		
//		if(userName.isEmpty() || password.isEmpty() || name.isEmpty() || lastName.isEmpty() ||
//				email.isEmpty() ||  question.isEmpty() || answer.isEmpty()
//				|| telephone.isEmpty() || celular.isEmpty() )
//			throw new IllegalArgumentException();
//		this.password = password;
//		this.name = name;
//		this.lastName = lastName;
//		this.email = email;
//		this.question = question;
//		this.answer = answer;
//		this.address = address;
//		this.telephone = telephone;
//		this.celular = celular; 
//		this.birthday = birthday;
//		this.isMale = isMale;		
//	}
	
	public String getPassword() {
		return password;
	}
	
	public int getPositive() {
		return positive;
	}

	public int getNegative() {
		return negative;
	}

	public int getNeutral() {
		return neutral;
	}
	
	public void recieveQualification(Calification c) {
		if (c.equals(Calification.positive)) {
			incPositive();
		} else if (c.equals(Calification.negative)) {
			incNegative();
		} else {
			incNeutral();
		}
	}
	private void incPositive(){
		positive++;
	}

	private void incNeutral(){
		neutral++;
	}
	
	private void incNegative(){
		negative++;
	}
	
	public String getName() {
		return name;
	}
	public String getLastName() {
		return lastName;
	}

	public String getEmail() {
		return email;
	}
	public String getUserName() {
		return userName;
	}
	public Date getLastAccess() {
		return lastAccess;
	}

	public boolean getIsBlock() {
		return isBlock;
	}

	public String getQuestion() {
		return question;
	}

	public String getAnswer() {
		return answer;
	}

	public String getAddress() {
		return address;
	}

	public String getTelephone() {
		return telephone;
	}

	public String getCelular() {
		return celular;
	}

	public Date getBirthday() {
		return birthday;
	}

	public boolean isMale() {
		return isMale;
	}

	public boolean isAdmin() {
		return admin;
	}

	public void setIsBlock(boolean isBlock) {
		this.isBlock = isBlock;
	}

	public void setLastAccess(Date accessDay) {
		this.lastAccess = accessDay;
	}
	
	public void setPassword(String password) {
		if(password==null)
			throw new RequiredFieldsException();
		if(password.isEmpty())
			throw new IllegalArgumentException();
		this.password = password;
	}

	public void setName(String name) {
		if(name==null)
			throw new RequiredFieldsException();
		if(name.isEmpty())
			throw new IllegalArgumentException();
		this.name = name;
	}

	public void setLastName(String lastName) {
		if(lastName==null)
			throw new RequiredFieldsException();
		if(lastName.isEmpty())
			throw new IllegalArgumentException();
		this.lastName = lastName;
	}

	public void setEmail(String email) {
		if(email==null)
			throw new RequiredFieldsException();
		if(email.isEmpty())
			throw new IllegalArgumentException();
		this.email = email;
	}

	public void setQuestion(String question) {
		if(question==null)
			throw new RequiredFieldsException();
		if(question.isEmpty())
			throw new IllegalArgumentException();
		this.question = question;
	}

	public void setAnswer(String answer) {
		if(answer==null)
			throw new RequiredFieldsException();
		if(answer.isEmpty())
			throw new IllegalArgumentException();
		this.answer = answer;
	}

	public void setAddress(String address) {
		if(address==null)
			throw new RequiredFieldsException();
		if(address.isEmpty())
			throw new IllegalArgumentException();
		this.address = address;
	}

	public void setTelephone(String telephone) {
		if(telephone==null)
			throw new RequiredFieldsException();
		if(telephone.isEmpty())
			throw new IllegalArgumentException();
		this.telephone = telephone;
	}

	public void setCelular(String celular) {
		if(celular==null)
			throw new RequiredFieldsException();
		if(celular.isEmpty())
			throw new IllegalArgumentException();
		this.celular = celular;
	}

	public void setBirthday(Date birthday) {
		if(birthday==null)
			throw new RequiredFieldsException();
		this.birthday = birthday;
	}

	public void setMale(boolean isMale) {
		this.isMale = isMale;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((email == null) ? 0 : email.hashCode());
		result = prime * result
				+ ((userName == null) ? 0 : userName.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (email == null) {
			if (other.email != null)
				return false;
		} else if (!email.equals(other.email))
			return false;
		if (userName == null) {
			if (other.userName != null)
				return false;
		} else if (!userName.equals(other.userName))
			return false;
		return true;
	}

	public boolean validateAnswer(String answer) {
		return this.answer.equals(answer);
	}

	public void updateBlockedStatus() {
		isBlock=!isBlock;
	}
}